package com.example.huainanlive.ui

import androidx.lifecycle.*
import com.demon.androidbasic.base.BaseViewModel
import com.demon.net.ResponseResult
import com.example.huainanlive.bean.*
import com.example.huainanlive.repository.DeviceRepository

import kotlinx.coroutines.launch

class HuiGuLiveViewModel : BaseViewModel() {

    private val deviceRepository = DeviceRepository()

    /**
     * 查询直播回顾页面
     */
    private val _getLiveHouseRecordBean = MutableLiveData<MutableList<GetLiveHouseRecordBeanItem>>()
    val getLiveHouseRecordBean: LiveData<MutableList<GetLiveHouseRecordBeanItem>> =
        _getLiveHouseRecordBean

    /**
     * 查询直播预告页面
     */
    private val _getLiveHousePreviewBean =
        MutableLiveData<MutableList<GetLiveHousePreviewBeanItem>>()
    val getLiveHousePreviewBean: LiveData<MutableList<GetLiveHousePreviewBeanItem>> =
        _getLiveHousePreviewBean

    /**
     * 查询直播回顾详情页面
     */
    private val _getLiveHouseRecordDetailBean = MutableLiveData<GetliveHouseRecordDetailBean>()
    val getLiveHouseRecordDetailBean: LiveData<GetliveHouseRecordDetailBean> =
        _getLiveHouseRecordDetailBean

    /**
     * 查询直播预告详情页面
     */
    private val _getLiveHouseDetailBean = MutableLiveData<GetLiveHouseDetailBean>()
    val getLiveHouseDetailBean: LiveData<GetLiveHouseDetailBean> = _getLiveHouseDetailBean

    /**
     * 主持人留言
     */
    private val _getCommentItemBean = MutableLiveData<MutableList<GetCommentItem>>()
    val getCommentItemBean: LiveData<MutableList<GetCommentItem>> = _getCommentItemBean

    /**
     * 用户留言
     */
    private val _getMessageBean = MutableLiveData<MutableList<GetCommentItem>>()
    val getMessageBean: LiveData<MutableList<GetCommentItem>> = _getMessageBean
    /**
     * 用户留言
     */
    private val _getWorldBean = MutableLiveData<MutableList<GetWorldItem>>()
    val getWorldBean: LiveData<MutableList<GetWorldItem>> = _getWorldBean
    /**
     * 添加预约是否成功
     */
    private val _addSub = MutableLiveData<Boolean>()
    val addSub: LiveData<Boolean> = _addSub
  /**
     * 添加预约是否成功
     */
    private val _enterLive= MutableLiveData<Boolean>()
    val enterLive: LiveData<Boolean> = _enterLive

    /**
     * 查询直播回顾页面
     */
    fun getLiveHouseRecord() {
        viewModelScope.launch {
            val result = mExecutor.executeRequest(call = {
                deviceRepository.liveHouseRecord()
            })
            if (result is ResponseResult.Success) {
                result.data?.let {
                    _getLiveHouseRecordBean.postValue(it)
                }
            }
        }
    }


    /**
     * 查询直播预告页面
     */
    fun getLiveHousePreview() {
        viewModelScope.launch {
            val result = mExecutor.executeRequest(call = {
                deviceRepository.liveHousePreview()
            })
            if (result is ResponseResult.Success) {
                result.data?.let {
                    _getLiveHousePreviewBean.postValue(it)
                }
            }
        }
    }


    /**
     * 查询直播回顾详情页面
     */
    fun liveHouseRecordDetail(recordId: Long = 0L) {
        viewModelScope.launch {
            val result = mExecutor.executeRequest(call = {
                deviceRepository.liveHouseRecordDetail(recordId)
            })
            if (result is ResponseResult.Success) {
                result.data?.let {
                    _getLiveHouseRecordDetailBean.postValue(it)
                }
            }
        }
    }

    /**
     * 主持人留言
     */
    fun getComment(
        liveId: Long = 0L,
        pageSize: Int = 20,
        pageNum: Int = 1
    ) {
        viewModelScope.launch {
            val result = mExecutor.executeRequest(call = {
                deviceRepository.getComment(liveId, pageSize, pageNum)
            })
            if (result is ResponseResult.Success) {
                result.data?.let {
                    _getCommentItemBean.postValue(it)
                }
            }
        }
    }

    /**
     * 用户留言
     */
    fun getMessage(
        liveId: Long = 0L,
        pageSize: Int = 20,
        pageNum: Int = 1
    ) {
        viewModelScope.launch {
            val result = mExecutor.executeRequest(call = {
                deviceRepository.getMessage(liveId, pageSize, pageNum)
            })
            if (result is ResponseResult.Success) {
                result.data?.let {
                    _getMessageBean.postValue(it)
                }
            }
        }
    }

    /**
     * 关键词
     */
    fun getWord(
        liveId: Long = 0L,
    ) {
        viewModelScope.launch {
            val result = mExecutor.executeRequest(call = {
                deviceRepository.getWord(liveId)
            })
            if (result is ResponseResult.Success) {
                result.data?.let {
                    _getWorldBean.postValue(it)
                }
            }
        }
    }

    /**
     * 添加预约
     */
    fun addSub(
        liveId: LiveIdBean?= null,
    ) {
        viewModelScope.launch {
            val result = mExecutor.executeRequest(call = {
                deviceRepository.addSub(liveId)
            })

            if (result is ResponseResult.Success) {
                _addSub.postValue(true)
                if (liveId != null) {
                    cancelSub(liveId)
                }

            }
        }
    }

    /**
     * 取消预约
     */
    fun cancelSub(
        liveId: LiveIdBean?= null,
    ) {
        viewModelScope.launch {
            val result = mExecutor.executeRequest(call = {
                deviceRepository.cancelSub(liveId)
            })

            if (result is ResponseResult.Success) {
                _addSub.postValue(true)
                deviceRepository.liveHousePreview()

            }
        }
    }
    /**
     * 进入直播间
     */
    fun enterLive(
        liveId: LiveIdBean?= null,
    ) {
        viewModelScope.launch {
            val result = mExecutor.executeRequest(call = {
                deviceRepository.enterLive(liveId)
            })

            if (result is ResponseResult.Success) {
                _enterLive.postValue(true)
            }
        }
    }
    /**
     * 留言
     */
    fun addMsg(
        addMsgBean:AddMsgBean? = null
    ) {
        viewModelScope.launch {
            val result = mExecutor.executeRequest(call = {
                deviceRepository.addMsg(addMsgBean)
            })

            if (result is ResponseResult.Success) {
                _enterLive.postValue(true)
            }
        }
    }

    /**
     * 离开直播间
     */
    fun leaveLive(
        addZanBean: LiveIdBean? = null
    ) {
        viewModelScope.launch {
            val result = mExecutor.executeRequest(call = {
                deviceRepository.leaveLive(addZanBean)
            })
        }
    }


    /**
     * 点赞
     */
    fun addZan(
        addMsgBean:AddZanBean? = null
    ) {
        viewModelScope.launch {
            val result = mExecutor.executeRequest(call = {
                deviceRepository.addZan(addMsgBean)
            })

            if (result is ResponseResult.Success) {
                _enterLive.postValue(true)
            }
        }
    }

    /**
     * 查询直播预告页面
     */
    fun liveHouseDetail(liveId: Long = 0L) {
        viewModelScope.launch {
            val result = mExecutor.executeRequest(call = {
                deviceRepository.liveHouseDetail(liveId)
            })
            if (result is ResponseResult.Success) {
                result.data?.let {
                    _getLiveHouseDetailBean.postValue(it)
                }
            }
        }
    }


    class HuiGuLiveVMFactory(
        private val factory: ViewModelProvider.Factory,
    ) : ViewModelProvider.Factory {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            if (modelClass.isAssignableFrom(HuiGuLiveViewModel::class.java)) {
                return HuiGuLiveViewModel() as T
            }
            return factory.create(modelClass)
        }
    }


}